package com.china.produce.controller;

import com.alibaba.fastjson.JSON;
import com.china.produce.annotation.Log;
import com.china.produce.annotation.LoginNeedless;
import com.china.produce.annotation.RateLimiter;
import com.china.produce.enums.BusinessType;
import com.china.produce.exception.ResultBody;
import com.china.produce.filter.PropertyPreExcludeFilter;
import com.china.produce.pojo.Student;
import com.china.produce.pojo.User;
import com.china.produce.req.ExcludeReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2023/3/8
 */
@RequestMapping("/fastJson")
@RestController
@Slf4j
public class FastjsonController {

    @GetMapping("/jsonField")
    @RequiresPermissions("info:a")
    public void testJsonField() {

        Student student = new Student();
        student.setId(10);
        student.setName("长江");
        student.setAddress("nanjing");
        student.setPhoneNumber("15618298639");

        String stu = JSON.toJSONString(student);
        log.info("stu : {}", stu);

        Student students = JSON.parseObject(stu, Student.class);

        log.info("students : {}", students);
    }

    @GetMapping("/loginNeedless/{id}")
    @LoginNeedless
    public String testLoginNeedless(@PathVariable("id") Integer id) {

        return "login need less !";
    }

    @PostMapping("/excludeProperty")
    @RateLimiter
    @Log(title = "第一模块", businessType = BusinessType.INSERT, excludeParamNames = {"account"} )
    public ResultBody excludeProperty(@RequestBody ExcludeReq req)
    {
        User user = new User();
        user.setId(req.getId());
        user.setAccount("nan jin");
        user.setPassword(req.getPassword());

        String[] d = {"account"};

        log.info("user : {}", JSON.toJSONString(user));

        String res = JSON.toJSONString(user, excludePropertyPreFilter(d));

        return ResultBody.success(res);
    }

    public PropertyPreExcludeFilter excludePropertyPreFilter(String[] excludeParamNames)
    {
        String[] abc = {"password"};

        return new PropertyPreExcludeFilter().addExcludes(ArrayUtils.addAll(abc, excludeParamNames));
    }

}
